package server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

/**
 * <p>Title: Environment.java</p>
 *
 * <p>Description: Helper class to save all the environment variables that are
 * needed to ensure proper configuration and execution of CGI scripts
 * </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author Luping May and Tracie Hong
 * @version 1.0
 */
public class Environment {
    private HttpdConf ConfFile;
    private Request ClientRequest;
    private Socket ClientSocket;
    private HashMap <String, String> envVariablesTables;
    private Vector <String> envVariablesVector;
    
  /**
   * Constructor for Environment object. Saves the information from the server
   * configuration file as well as from the request into a data structure so if
   * the request is for a cgi script, it can be executed properly.
   *
   * @param c HttpdConf file which contains some variables which need to be
   *   saved in order for cgi scripts to execute properly.
   * @param r Request object which contains request specific variables needed
   *   to execute cgi scripts
   * @param cl Socket used by the client to connect to the server
   */
  public Environment(HttpdConf c, Request r, Socket cl) {
    ConfFile = c;
    ClientRequest = r;
    ClientSocket = cl;
    envVariablesVector = new Vector();
    envVariablesTables = new HashMap();
  }

  /**
   * Function to save static variables into your data structure which can be
   * then used during script execution. Variables that can also be accessed
   * directly from the request object or the socket (objects passed in during
   * instantiation) can be saved here as well.
   * HINT: Most request variables that need to be saved need to have an HTTP_
   * variable name so take this into consideration when planning how to design
   * and save environment variables.
   */
  public void setEnv() throws FileNotFoundException, IOException {
    InetAddress ip = ClientSocket.getInetAddress();
    envVariablesTables.put("REMOTE_HOST", ClientRequest.getENV("Host:"));
    envVariablesTables.put("REMOTE_ADDR",ip.getHostAddress());
    envVariablesTables.put("AUTH_TYPE","");

    envVariablesTables.put("REMOTE_USER",ip.getHostName());
    envVariablesTables.put("REMOTE_IDENT"," ");
    envVariablesTables.put("CONTENT_TYPE",ClientRequest.getENV("CONTENT-TYPE:"));
//    FileInputStream temp = new FileInputStream(new File(ClientRequest.getURI()));
//    envVariablesTables.put("CONTENT_LENGTH",Integer.toString(temp.available()));
    envVariablesTables.put("CONTENT_LENGTH",ClientRequest.getENV("CONTENT-LENGTH:"));
    envVariablesTables.put("SERVER_SOFTWARE","Jun & Lisa Server v2.0");
    envVariablesTables.put("SERVER_NAME","Jun & Lisa Server");
    envVariablesTables.put("SERVER_PORT",Integer.toString(ConfFile.getListen()));
    envVariablesTables.put("GATEWAY_INTERFACE","CGI/1.1");
    envVariablesTables.put("SERVER_PROTOCOL",ClientRequest.getVersion());
    envVariablesTables.put("QUERY_STRING",ClientRequest.getQuery());
    envVariablesTables.put("PATH_INFO",ClientRequest.getPathInfo());
    envVariablesTables.put("PATH_TRANSLATED", ClientRequest.getPathTranslated());
    envVariablesTables.put("SCRIPT_NAME",ClientRequest.getSCRIPTNAME());
    envVariablesTables.put("HTTP_USER_AGENT",ClientRequest.getENV("User-Agent:"));
    envVariablesTables.put("HTTP_ACCEPT",ClientRequest.getENV("Accept:"));
    envVariablesTables.put("HTTP_REFERER",ClientRequest.getENV("Referer:"));
    envVariablesTables.put("REQUEST_METHOD",ClientRequest.getMethod());
    envVariablesTables.put("SERVER_ADMIN",ConfFile.getServerAdmin());


//    String key, value;
//    for (Enumeration e = envVariablesTables.keys(); e.hasMoreElements(); ) {
//        key = (String)e.nextElement();
//        value = (String)envVariablesTables.get(key);
//        key = key + "=" + value;
//        addEnvVar(key);
//    }
  }

  /**
   * Function to return a data structure that contains all the environment
   * variables that were saved.
   *
   * @return String[]. Chose as data type upon return due to parsing of variable
   *   content and name which both need to be stored.
   */
//  public String[] getEnv() {
//    String ENV[] = new String[envVariablesVector.size()];
//    for(int i=0; i<envVariablesVector.size(); i++){
//        ENV[i] = envVariablesVector.elementAt(i);
//    }
//    return ENV;
//  }

  public HashMap getEnv(){
      return envVariablesTables;
  }

  /**
   * Used to add another variable to the data structure. Usually used when the
   * data has not be stored yet through the setEnv function.
   *
   * @param var String that contains both the variable name and data. Used this
   *   way for parsing purposes later on.
   */
  public void addEnvVar(String Key,String Value) {
    envVariablesTables.put(Key, Value);
  }
}